//
//  HelloIphoneViewController.m
//  HelloIphone
//
//  Created by Geoffrey Gallaway on 12/16/08.
//  Copyright TDK Technologies 2008. All rights reserved.
//

#import "HelloIphoneViewController.h"
#import "WeightedRandomTextGenerator.h"
#import "Beep.h"
#import "Tone.h" 
#import "MorseCode.h"

@implementation HelloIphoneViewController

@synthesize audioPlayer;


-(IBAction)hello:(id)sender {
	//helloLabel.text = [@"Hello: " stringByAppendingString:nameField.text];
	
	NSArray *characterList = [NSArray arrayWithObjects: @"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L", @"M",
							  @"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"0",@"1",@"2",@"3",
							  @"4",@"5",@"6",@"7",@"8",@"9",@" ",nil];
	WeightedRandomTextGenerator *randomGenerator = [[WeightedRandomTextGenerator alloc] initWithCharacterList:characterList];
	[randomGenerator buildWeightedItemList:200];
	NSArray *randomCharacterList = [randomGenerator weightedRandomCharacterList];
	
	MorseCode *morseCode = [[MorseCode alloc] init];
	morseCode.dit = dit;
	morseCode.dah = dah;
	morseCode.toneDelay = toneDelay;
	morseCode.letterDelay = letterDelay;
	
	helloLabel.text = @"";
	
	
	UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
	AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);
	

	Beep *theBeeper = [[Beep alloc] init];
	
	if (theBeeper) {
		self.audioPlayer = theBeeper;
		//[theBeeper release];
		AudioSessionSetActive(true);

	}
	
	NSMutableArray *tones = [[NSMutableArray alloc] init];	
	for (int i = 0; i < [randomCharacterList count]; i++) {
		NSString *character = [randomCharacterList objectAtIndex:i];
		[morseCode stringToTonesList:character tones:tones];
		[theBeeper addTones:tones];
		//[theBeeper addTone:letterDelay];

		helloLabel.text = character;
		
		//while ([theBeeper isDonePlaying] == true) {
			[theBeeper play];			
		//}
		[theBeeper removeTones];
		[tones removeAllObjects];
		
	}
	[randomGenerator release];
	[randomCharacterList release];
	
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	dit = [[Tone alloc] initWithFrequency:750 duration:100 volume:100];
	[dit generateTone];
	dah = [[Tone alloc] initWithFrequency:750 duration:250 volume:100];
	[dah generateTone];
	
	toneDelay = [[Tone alloc] initWithFrequency:0 duration:100 volume:100];
	[toneDelay generateTone];

	letterDelay = [[Tone alloc] initWithFrequency:0 duration:200 volume:100];
	[letterDelay generateTone];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}

@end
